package concurrent;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 锁可以相应中断而sync不行
 */
public class InterruptTest2 extends Thread {

    public static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        InterruptTest2 a = new InterruptTest2();
        lock.lockInterruptibly();
        a.start();
        Thread.sleep(1000);
        a.interrupt();
        Thread.sleep(3000);
        lock.unlock();
        System.out.println("main结束");
    }

    @Override
    public void run() {
        try {
            System.out.println("run");
            lock.lockInterruptibly();
            System.out.println("进入sync");
            lock.unlock();
            System.out.println("退出sync");
        } catch (Exception e) {
            System.out.println("exception");
            e.printStackTrace();
        }
    }
}
